<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>PayPal支付对接 | 指尖改变世界</title><meta name="keywords" content="PayPal支付对接"><meta name="author" content="沙屿沫"><meta name="copyright" content="沙屿沫"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="PayPal支付对接">
<meta property="og:type" content="article">
<meta property="og:title" content="PayPal支付对接">
<meta property="og:url" content="http://blog.shayvmo.cn/2021/09/28/paypal/index.html">
<meta property="og:site_name" content="指尖改变世界">
<meta property="og:description" content="PayPal支付对接">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://blog.shayvmo.cn/images/code.jpg">
<meta property="article:published_time" content="2021-09-28T03:59:12.000Z">
<meta property="article:modified_time" content="2021-09-28T03:59:12.000Z">
<meta property="article:author" content="沙屿沫">
<meta property="article:tag" content="Paypal">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://blog.shayvmo.cn/images/code.jpg"><link rel="shortcut icon" href="/images/avatar.jpg"><link rel="canonical" href="http://blog.shayvmo.cn/2021/09/28/paypal/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?5e4d1e8fcc524b4e732ea58fa9fa15f0";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'PayPal支付对接',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-09-28 11:59:12'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src= "" data-lazy-src="/images/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">74</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">42</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/images/code.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">指尖改变世界</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">PayPal支付对接</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-09-28T03:59:12.000Z" title="发表于 2021-09-28 11:59:12">2021-09-28</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-09-28T03:59:12.000Z" title="更新于 2021-09-28 11:59:12">2021-09-28</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%85%B6%E4%BB%96/">其他</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.4k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>13分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="PayPal支付对接"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="PayPal支付对接"><a href="#PayPal支付对接" class="headerlink" title="PayPal支付对接"></a>PayPal支付对接</h2><p>背景：香港PC商城接入PayPal支付。</p>
<h3 id="支付流程"><a href="#支付流程" class="headerlink" title="支付流程"></a>支付流程</h3><p>1、当点击PayPal 按钮的时候，会触发js的CreateOrder函数，调用Create Payment(pyament.php)后台的URL。</p>
<p>2、在调用Create Payment 后台的URL上，调用PayPal Create Payment API。调用API 请求（<a target="_blank" rel="noopener" href="https://developer.paypal.com/docs/api/payments/v1/#payment_create">https://developer.paypal.com/docs/api/payments/v1/#payment_create</a>）</p>
<p>3、返回token（res.token）。到这里整个Create Order函数结束，如果API调用成功，会弹窗并跳转到PayPal登陆页面。</p>
<p>4、PayPal登陆以后，点击付款按钮时，会触发onApprove方法。</p>
<p>5、传payment id，token, payer id给Execute Payment(successful.php)后台的URL。这个url是在调用Create Payment的时候设置的return_url</p>
<p>6、在Execute Payment后台的URL上调用PayPal的 Execute Payment API。(<a target="_blank" rel="noopener" href="https://developer.paypal.com/docs/api/payments/v1/#payment_execute">https://developer.paypal.com/docs/api/payments/v1/#payment_execute</a>)</p>
<h3 id="创建沙箱账号"><a href="#创建沙箱账号" class="headerlink" title="创建沙箱账号"></a>创建沙箱账号</h3><p>1、在 <a target="_blank" rel="noopener" href="https://www.paypal.com">www.paypal.com</a> 官方网站创建一个账号（开发调试的话，创建个人账号即可）。</p>
<p>2、登录 <a target="_blank" rel="noopener" href="https://developer.paypal.com">developer.paypal.com</a></p>
<p>3、点击左侧菜单栏的<code>SANDBOX</code>下的 <code>Accounts</code> 可以看到系统默认创建的2个账号。这2个账号都是可以编辑信息的，当然也可以使用默认的信息。</p>
<ul>
<li><code>Business</code> 是商家账号（后面商家账号会用到设置异步回调通知）</li>
<li><code>Personal</code> 是个人账号（用于调试支付时登录）</li>
</ul>
<p><img src= "" data-lazy-src="/images/paypal/accounts.png" alt=""></p>
<h3 id="创建应用"><a href="#创建应用" class="headerlink" title="创建应用"></a>创建应用</h3><p>这里应用的概念，在我理解和支付宝支付里面创建的应用差不多，可以看做是使用PayPal支付的一个主体，一个商家账号下面可以有多个主体。</p>
<p><img src= "" data-lazy-src="/images/paypal/app.png" alt=""></p>
<p>点开<code>Default Application</code>这个应用，可以看到关联的沙箱账号，<code>clientID</code> 和 <code>clientSecret</code>，这两个参数用于后续请求支付参数等操作。</p>
<p><img src= "" data-lazy-src="/images/paypal/app-detail.png" alt=""></p>
<h3 id="异步通知地址设置"><a href="#异步通知地址设置" class="headerlink" title="异步通知地址设置"></a>异步通知地址设置</h3><p>使用上面提到的沙箱商家账号登录沙箱后台地址 <a target="_blank" rel="noopener" href="https://www.sandbox.paypal.com/">https://www.sandbox.paypal.com/</a>，设置即时通知地址即可。</p>
<p><img src= "" data-lazy-src="/images/paypal/sandbox-business.png" alt=""></p>
<p><img src= "" data-lazy-src="/images/paypal/sandbox-business-1.png" alt=""></p>
<p><img src= "" data-lazy-src="/images/paypal/sandbox-business-2.png" alt=""></p>
<h3 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h3><p>【官方开源SDK】<a target="_blank" rel="noopener" href="https://github.com/paypal/PayPal-PHP-SDK">https://github.com/paypal/PayPal-PHP-SDK</a></p>
<p>sample 目录下是各种不同的示例。</p>
<p><img src= "" data-lazy-src="/images/paypal/sample.png" alt=""></p>
<p>以下是我在项目中的代码。</p>
<p>示例HTML</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://www.paypal.com/sdk/js?client-id=上面提到的client-id&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;paypal-button-container&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">   paypal.Buttons(&#123;</span></span><br><span class="line"><span class="javascript">      <span class="attr">style</span>:&#123;</span></span><br><span class="line"><span class="javascript">        <span class="attr">color</span>:<span class="string">&#x27;silver&#x27;</span>, <span class="comment">//gold, blue, silver, white, black</span></span></span><br><span class="line"><span class="javascript">        <span class="attr">size</span>: <span class="string">&#x27;small&#x27;</span></span></span><br><span class="line"><span class="javascript">      &#125;,</span></span><br><span class="line"><span class="javascript">      <span class="attr">commit</span>:<span class="literal">true</span>,</span></span><br><span class="line"><span class="javascript">      <span class="attr">createOrder</span>: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> fetch(<span class="string">&#x27;createOrder的地址&#x27;</span>, &#123;</span></span><br><span class="line"><span class="javascript">          <span class="attr">method</span>: <span class="string">&#x27;get&#x27;</span>,</span></span><br><span class="line"><span class="javascript">          <span class="attr">headers</span>: &#123;</span></span><br><span class="line"><span class="javascript">            <span class="string">&#x27;content-type&#x27;</span>: <span class="string">&#x27;application/json&#x27;</span></span></span><br><span class="line"><span class="javascript">          &#125;</span></span><br><span class="line"><span class="javascript">        &#125;).then(<span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">          <span class="keyword">return</span> res.json();</span></span><br><span class="line"><span class="javascript">        &#125;).then(<span class="function"><span class="keyword">function</span> (<span class="params">data</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">let</span> token;</span></span><br><span class="line"><span class="javascript">            token = data.data.approval_url.match(<span class="regexp">/EC-\w+/</span>)[<span class="number">0</span>];</span></span><br><span class="line"><span class="javascript">            <span class="keyword">return</span> token;</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">      &#125;,</span></span><br><span class="line"><span class="javascript">      <span class="attr">onApprove</span>: <span class="function"><span class="keyword">function</span> (<span class="params">data</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> DOEC_URL = <span class="string">&#x27;execute的地址&#x27;</span>;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> fetch(DOEC_URL, &#123;</span></span><br><span class="line"><span class="javascript">          <span class="attr">method</span>: <span class="string">&#x27;post&#x27;</span>,</span></span><br><span class="line"><span class="javascript">          <span class="attr">headers</span>: &#123;</span></span><br><span class="line"><span class="javascript">            <span class="string">&#x27;content-type&#x27;</span>: <span class="string">&#x27;application/json&#x27;</span></span></span><br><span class="line"><span class="javascript">          &#125;,</span></span><br><span class="line"><span class="javascript">          <span class="attr">body</span>: <span class="built_in">JSON</span>.stringify(&#123;</span></span><br><span class="line"><span class="javascript">            <span class="attr">paymentId</span>: data.paymentID,</span></span><br><span class="line"><span class="javascript">            <span class="attr">token</span>: data.orderID,</span></span><br><span class="line"><span class="javascript">            <span class="attr">payerID</span>: data.payerID</span></span><br><span class="line"><span class="javascript">          &#125;)</span></span><br><span class="line"><span class="javascript">        &#125;).then(<span class="function"><span class="keyword">function</span>(<span class="params">res</span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">          <span class="keyword">return</span> res.json();</span></span><br><span class="line"><span class="javascript">        &#125;).then(<span class="function"><span class="keyword">function</span>(<span class="params">data</span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">          <span class="built_in">console</span>.log(data);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">      &#125;</span></span><br><span class="line"><span class="javascript">    &#125;).render(<span class="string">&#x27;#paypal-button-container&#x27;</span>);</span></span><br><span class="line"><span class="javascript">  </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>后端代码类(PHP)</p>
<figure class="highlight php"><table><tr><td class="code"><pre><span class="line"><span class="keyword">namespace</span> <span class="title">app</span>\<span class="title">services</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">PayPal</span>\<span class="title">Api</span>\<span class="title">Amount</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">PayPal</span>\<span class="title">Api</span>\<span class="title">Details</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">PayPal</span>\<span class="title">Api</span>\<span class="title">FlowConfig</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">PayPal</span>\<span class="title">Api</span>\<span class="title">Payer</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">PayPal</span>\<span class="title">Api</span>\<span class="title">Payment</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">PayPal</span>\<span class="title">Api</span>\<span class="title">PaymentExecution</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">PayPal</span>\<span class="title">Api</span>\<span class="title">RedirectUrls</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">PayPal</span>\<span class="title">Api</span>\<span class="title">RefundRequest</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">PayPal</span>\<span class="title">Api</span>\<span class="title">Sale</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">PayPal</span>\<span class="title">Api</span>\<span class="title">Transaction</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PayPalService</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="variable">$clientId</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="variable">$clientSecret</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="variable">$is_live</span> = <span class="literal">false</span>;<span class="comment">// 生产环境</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="variable">$returnUrl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="variable">$cancelUrl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="variable">$notifyUrl</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="variable">$currency</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">$this</span>-&gt;clientId = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">		<span class="keyword">$this</span>-&gt;clientSecret = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">		<span class="keyword">$this</span>-&gt;currency = <span class="string">&#x27;USD&#x27;</span>;<span class="comment">// HKD 港币 USD 美元</span></span><br><span class="line">        <span class="keyword">$this</span>-&gt;returnUrl = <span class="string">&#x27;&#x27;</span>;<span class="comment">//批准付款后重定向地址</span></span><br><span class="line">        <span class="keyword">$this</span>-&gt;cancelUrl = <span class="string">&#x27;&#x27;</span>;<span class="comment">//取消付款后重定向地址</span></span><br><span class="line">		<span class="keyword">$this</span>-&gt;notifyUrl = <span class="string">&#x27;&#x27;</span>;<span class="comment">// IPN URL 异步通知地址</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Description</span> 创建订单</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Author</span> 沙屿沫</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Date</span> 2021/9/26 15:39</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> array $params</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> array</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">createOrder</span>(<span class="params"><span class="keyword">array</span> <span class="variable">$params</span></span>): <span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        [</span><br><span class="line">            <span class="string">&#x27;order_sn&#x27;</span> =&gt; <span class="variable">$order_sn</span>,</span><br><span class="line">            <span class="string">&#x27;description&#x27;</span> =&gt; <span class="variable">$description</span>,</span><br><span class="line">            <span class="string">&#x27;total&#x27;</span> =&gt; <span class="variable">$total</span>,</span><br><span class="line">            <span class="string">&#x27;subtotal&#x27;</span> =&gt; <span class="variable">$subtotal</span>,</span><br><span class="line">            <span class="string">&#x27;shipping&#x27;</span> =&gt; <span class="variable">$shipping</span>,</span><br><span class="line">            <span class="string">&#x27;shipping_discount&#x27;</span> =&gt; <span class="variable">$shipping_discount</span>,</span><br><span class="line">            <span class="string">&#x27;handling_fee&#x27;</span> =&gt; <span class="variable">$handling_fee</span>,</span><br><span class="line">            <span class="string">&#x27;insurance&#x27;</span> =&gt; <span class="variable">$insurance</span>,</span><br><span class="line">            <span class="string">&#x27;tax&#x27;</span> =&gt; <span class="variable">$tax</span>,</span><br><span class="line">        ] = <span class="variable">$params</span>;</span><br><span class="line">        <span class="variable">$demo_params</span> = [</span><br><span class="line">            <span class="string">&#x27;order_sn&#x27;</span> =&gt; <span class="string">&#x27;&#x27;</span>,<span class="comment">// 必填</span></span><br><span class="line">            <span class="string">&#x27;description&#x27;</span> =&gt; <span class="string">&#x27;&#x27;</span>,<span class="comment">// 建议必填</span></span><br><span class="line">            <span class="string">&#x27;total&#x27;</span> =&gt; <span class="string">&#x27;&#x27;</span>,<span class="comment">// 必填，全部总额（包含手续费等费用）</span></span><br><span class="line">            <span class="string">&#x27;subtotal&#x27;</span> =&gt; <span class="string">&#x27;&#x27;</span>,<span class="comment">// 必填，总额（除其他费用）</span></span><br><span class="line">            <span class="string">&#x27;shipping&#x27;</span> =&gt; <span class="string">&#x27;&#x27;</span>,<span class="comment">// 建议必填，运费</span></span><br><span class="line">            <span class="string">&#x27;shipping_discount&#x27;</span> =&gt; <span class="string">&#x27;&#x27;</span>,<span class="comment">// 选填，运费折扣，负数减</span></span><br><span class="line">            <span class="string">&#x27;handling_fee&#x27;</span> =&gt; <span class="string">&#x27;&#x27;</span>,<span class="comment">// 选填，手续费</span></span><br><span class="line">            <span class="string">&#x27;insurance&#x27;</span> =&gt; <span class="string">&#x27;&#x27;</span>,<span class="comment">// 选填，保险费</span></span><br><span class="line">            <span class="string">&#x27;tax&#x27;</span> =&gt; <span class="string">&#x27;&#x27;</span>,<span class="comment">// 选填，税费</span></span><br><span class="line">        ];</span><br><span class="line"></span><br><span class="line">        <span class="variable">$apiContext</span> = <span class="keyword">new</span> \PayPal\Rest\ApiContext(</span><br><span class="line">            <span class="keyword">new</span> \PayPal\Auth\OAuthTokenCredential(</span><br><span class="line">                <span class="keyword">$this</span>-&gt;clientId,<span class="comment">//client ID</span></span><br><span class="line">                <span class="keyword">$this</span>-&gt;clientSecret <span class="comment">//secret ID</span></span><br><span class="line">            )</span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">$this</span>-&gt;is_live) &#123;</span><br><span class="line">            <span class="variable">$apiContext</span>-&gt;setConfig(<span class="keyword">array</span>(<span class="string">&#x27;mode&#x27;</span>=&gt;<span class="string">&#x27;live&#x27;</span>));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="variable">$flow</span> = <span class="keyword">new</span> FlowConfig();</span><br><span class="line">        <span class="variable">$flow</span>-&gt;setLandingPageType(<span class="string">&#x27;Billing&#x27;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="variable">$payer</span> = <span class="keyword">new</span> Payer();</span><br><span class="line">        <span class="variable">$payer</span> -&gt; setPaymentMethod(<span class="string">&#x27;paypal&#x27;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="variable">$redirectUrls</span> = <span class="keyword">new</span> RedirectUrls();</span><br><span class="line">        <span class="variable">$redirectUrls</span> -&gt; setReturnUrl(<span class="keyword">$this</span>-&gt;returnUrl)</span><br><span class="line">            -&gt; setCancelUrl(<span class="keyword">$this</span>-&gt;cancelUrl);</span><br><span class="line"></span><br><span class="line">        <span class="variable">$details</span> = <span class="keyword">new</span> Details();</span><br><span class="line"></span><br><span class="line">        <span class="variable">$shipping_discount</span> &amp;&amp; <span class="variable">$details</span>-&gt;setShippingDiscount(<span class="variable">$shipping_discount</span>);</span><br><span class="line">        <span class="variable">$handling_fee</span> &amp;&amp; <span class="variable">$details</span>-&gt;setHandlingFee(<span class="variable">$handling_fee</span>);</span><br><span class="line">        <span class="variable">$insurance</span> &amp;&amp; <span class="variable">$details</span>-&gt;setInsurance(<span class="variable">$insurance</span>);</span><br><span class="line">        <span class="variable">$tax</span> &amp;&amp; <span class="variable">$details</span>-&gt;setTax(<span class="variable">$tax</span>);</span><br><span class="line"></span><br><span class="line">        <span class="variable">$details</span>-&gt;setShipping(<span class="variable">$shipping</span>)</span><br><span class="line">            -&gt;setSubtotal(<span class="variable">$subtotal</span>);</span><br><span class="line"></span><br><span class="line">        <span class="variable">$amount</span> = <span class="keyword">new</span> Amount();</span><br><span class="line">        <span class="variable">$amount</span>-&gt;setCurrency(<span class="keyword">$this</span>-&gt;currency)-&gt;setTotal(<span class="variable">$total</span>)-&gt;setDetails(<span class="variable">$details</span>);</span><br><span class="line"></span><br><span class="line">        <span class="variable">$transaction</span> = <span class="keyword">new</span> Transaction();</span><br><span class="line">        <span class="variable">$transaction</span> -&gt; setAmount(<span class="variable">$amount</span>)</span><br><span class="line">            -&gt; setDescription(<span class="variable">$description</span>)</span><br><span class="line">            -&gt; setInvoiceNumber(<span class="variable">$order_sn</span>); <span class="comment">//订单Id</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">$this</span>-&gt;notifyUrl &amp;&amp; <span class="variable">$transaction</span>-&gt;setNotifyUrl(<span class="keyword">$this</span>-&gt;notifyUrl); <span class="comment">//IPN URL</span></span><br><span class="line"></span><br><span class="line">        <span class="variable">$payment</span> = <span class="keyword">new</span> Payment();</span><br><span class="line">        <span class="variable">$payment</span> -&gt; setIntent(<span class="string">&#x27;sale&#x27;</span>)</span><br><span class="line">            -&gt; setPayer(<span class="variable">$payer</span>)</span><br><span class="line">            -&gt; setRedirectUrls(<span class="variable">$redirectUrls</span>)</span><br><span class="line">            -&gt; setTransactions(<span class="keyword">array</span>(<span class="variable">$transaction</span>));</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span>&#123;</span><br><span class="line"></span><br><span class="line">            <span class="variable">$payment</span> -&gt; create(<span class="variable">$apiContext</span>);</span><br><span class="line">            <span class="variable">$approval_url</span> = <span class="variable">$payment</span> -&gt; getApprovalLink();</span><br><span class="line">            <span class="keyword">return</span> compact(<span class="string">&#x27;approval_url&#x27;</span>);<span class="comment">// 根据示例html </span></span><br><span class="line"><span class="comment">//            return json_decode($payment-&gt;toJSON(128), true);</span></span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">catch</span>(\PayPal\<span class="built_in">Exception</span>\PayPalConnectionException <span class="variable">$ex</span>)&#123;</span><br><span class="line">            \Yii::error(<span class="string">&#x27;PayPalConnectionException: &#x27;</span>. <span class="variable">$ex</span>-&gt;getCode().<span class="string">&#x27; --- &#x27;</span>.<span class="variable">$ex</span>-&gt;getData());</span><br><span class="line">            <span class="keyword">throw</span> <span class="variable">$ex</span>;</span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">catch</span> (\<span class="built_in">Exception</span> <span class="variable">$ex</span>)&#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="variable">$ex</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Description</span> 确认扣款</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Author</span> 沙屿沫</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Date</span> 2021/9/26 15:39</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> string $payerId</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> string $paymentId</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> array</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">execute</span>(<span class="params"><span class="keyword">string</span> <span class="variable">$payerId</span>, <span class="keyword">string</span> <span class="variable">$paymentId</span></span>): <span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="variable">$apiContext</span> = <span class="keyword">new</span> \PayPal\Rest\ApiContext(</span><br><span class="line">            <span class="keyword">new</span> \PayPal\Auth\OAuthTokenCredential(</span><br><span class="line">                <span class="keyword">$this</span>-&gt;clientId,<span class="comment">//client ID</span></span><br><span class="line">                <span class="keyword">$this</span>-&gt;clientSecret <span class="comment">//secret ID</span></span><br><span class="line">            )</span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">$this</span>-&gt;is_live) &#123;</span><br><span class="line">            <span class="variable">$apiContext</span>-&gt;setConfig(<span class="keyword">array</span>(<span class="string">&#x27;mode&#x27;</span>=&gt;<span class="string">&#x27;live&#x27;</span>));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="variable">$payment</span> = Payment::get(<span class="variable">$paymentId</span>, <span class="variable">$apiContext</span>);</span><br><span class="line">        <span class="variable">$execution</span> = <span class="keyword">new</span> PaymentExecution();</span><br><span class="line">        <span class="variable">$execution</span> -&gt; setPayerId(<span class="variable">$payerId</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="variable">$result</span> = <span class="variable">$payment</span> -&gt; execute(<span class="variable">$execution</span>, <span class="variable">$apiContext</span>);</span><br><span class="line">            <span class="keyword">return</span> json_decode(<span class="variable">$result</span>-&gt;toJSON(<span class="number">128</span>), <span class="literal">true</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (\PayPal\<span class="built_in">Exception</span>\PayPalConnectionException <span class="variable">$ex</span>) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="variable">$ex</span>;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (\<span class="built_in">Exception</span> <span class="variable">$ex</span>)&#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="variable">$ex</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Description</span>查询订单</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Author</span> 沙屿沫</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Date</span> 2021/9/26 15:39</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> string $order_sn</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">query</span>(<span class="params"><span class="keyword">string</span> <span class="variable">$sale_id</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="variable">$apiContext</span> = <span class="keyword">new</span> \PayPal\Rest\ApiContext(</span><br><span class="line">            <span class="keyword">new</span> \PayPal\Auth\OAuthTokenCredential(</span><br><span class="line">                <span class="keyword">$this</span>-&gt;clientId,<span class="comment">//client ID</span></span><br><span class="line">                <span class="keyword">$this</span>-&gt;clientSecret <span class="comment">//secret ID</span></span><br><span class="line">            )</span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">$this</span>-&gt;is_live) &#123;</span><br><span class="line">            <span class="variable">$apiContext</span>-&gt;setConfig(<span class="keyword">array</span>(<span class="string">&#x27;mode&#x27;</span>=&gt;<span class="string">&#x27;live&#x27;</span>));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="variable">$demo_result</span> = <span class="string">&#x27;&#123;&quot;id&quot;:&quot;1DU410917H5745019&quot;,&quot;state&quot;:&quot;completed&quot;,&quot;amount&quot;:&#123;&quot;total&quot;:&quot;0.99&quot;,&quot;currency&quot;:&quot;USD&quot;,&quot;details&quot;:&#123;&quot;subtotal&quot;:&quot;0.99&quot;&#125;&#125;,&quot;payment_mode&quot;:&quot;INSTANT_TRANSFER&quot;,&quot;protection_eligibility&quot;:&quot;ELIGIBLE&quot;,&quot;protection_eligibility_type&quot;:&quot;ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE&quot;,&quot;transaction_fee&quot;:&#123;&quot;value&quot;:&quot;0.33&quot;,&quot;currency&quot;:&quot;USD&quot;&#125;,&quot;invoice_number&quot;:&quot;HM4cf42be81cf793d8c7b54a01e99e87&quot;,&quot;parent_payment&quot;:&quot;PAYID-MFIBXZQ2F2665569K055620G&quot;,&quot;create_time&quot;:&quot;2021-09-26T07:06:32Z&quot;,&quot;update_time&quot;:&quot;2021-09-26T07:06:32Z&quot;,&quot;links&quot;:[&#123;&quot;href&quot;:&quot;https://api.sandbox.paypal.com/v1/payments/sale/1DU410917H5745019&quot;,&quot;rel&quot;:&quot;self&quot;,&quot;method&quot;:&quot;GET&quot;&#125;,&#123;&quot;href&quot;:&quot;https://api.sandbox.paypal.com/v1/payments/sale/1DU410917H5745019/refund&quot;,&quot;rel&quot;:&quot;refund&quot;,&quot;method&quot;:&quot;POST&quot;&#125;,&#123;&quot;href&quot;:&quot;https://api.sandbox.paypal.com/v1/payments/payment/PAYID-MFIBXZQ2F2665569K055620G&quot;,&quot;rel&quot;:&quot;parent_payment&quot;,&quot;method&quot;:&quot;GET&quot;&#125;]&#125;&#x27;</span>;</span><br><span class="line">            <span class="variable">$result</span> = Sale::get(<span class="variable">$sale_id</span>, <span class="variable">$apiContext</span>);</span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">catch</span> (\<span class="built_in">Exception</span> <span class="variable">$exception</span>) &#123;</span><br><span class="line">            <span class="keyword">print</span>(<span class="variable">$exception</span>);</span><br><span class="line">            <span class="keyword">exit</span>();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> json_decode(<span class="variable">$result</span>-&gt;toJSON(<span class="number">128</span>), <span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Description</span> 订单退款</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Author</span> 沙屿沫</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Date</span> 2021/9/26 15:39</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">refund</span>(<span class="params"><span class="keyword">string</span> <span class="variable">$sale_id</span>, <span class="keyword">string</span> <span class="variable">$refund_money</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="variable">$amt</span> = <span class="keyword">new</span> Amount();</span><br><span class="line">        <span class="variable">$amt</span>-&gt;setCurrency(<span class="keyword">$this</span>-&gt;currency)</span><br><span class="line">            -&gt;setTotal(<span class="variable">$refund_money</span>);</span><br><span class="line"></span><br><span class="line">        <span class="variable">$refundRequest</span> = <span class="keyword">new</span> RefundRequest();</span><br><span class="line">        <span class="variable">$refundRequest</span>-&gt;setAmount(<span class="variable">$amt</span>);</span><br><span class="line"></span><br><span class="line">        <span class="variable">$sale</span> = <span class="keyword">new</span> Sale();</span><br><span class="line">        <span class="variable">$sale</span>-&gt;setId(<span class="variable">$sale_id</span>);</span><br><span class="line"></span><br><span class="line">        <span class="variable">$apiContext</span> = <span class="keyword">new</span> \PayPal\Rest\ApiContext(</span><br><span class="line">            <span class="keyword">new</span> \PayPal\Auth\OAuthTokenCredential(</span><br><span class="line">                <span class="keyword">$this</span>-&gt;clientId,<span class="comment">//client ID</span></span><br><span class="line">                <span class="keyword">$this</span>-&gt;clientSecret <span class="comment">//secret ID</span></span><br><span class="line">            )</span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">$this</span>-&gt;is_live) &#123;</span><br><span class="line">            <span class="variable">$apiContext</span>-&gt;setConfig(<span class="keyword">array</span>(<span class="string">&#x27;mode&#x27;</span>=&gt;<span class="string">&#x27;live&#x27;</span>));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="variable">$demo_result</span> = <span class="string">&#x27;&#123;&quot;id&quot;:&quot;9WW2694970862104R&quot;,&quot;state&quot;:&quot;completed&quot;,&quot;amount&quot;:&#123;&quot;total&quot;:&quot;0.99&quot;,&quot;currency&quot;:&quot;USD&quot;&#125;,&quot;refund_from_received_amount&quot;:&#123;&quot;value&quot;:&quot;0.96&quot;,&quot;currency&quot;:&quot;USD&quot;&#125;,&quot;refund_from_transaction_fee&quot;:&#123;&quot;value&quot;:&quot;0.03&quot;,&quot;currency&quot;:&quot;USD&quot;&#125;,&quot;total_refunded_amount&quot;:&#123;&quot;value&quot;:&quot;0.99&quot;,&quot;currency&quot;:&quot;USD&quot;&#125;,&quot;parent_payment&quot;:&quot;PAYID-MFIBXZQ2F2665569K055620G&quot;,&quot;sale_id&quot;:&quot;1DU410917H5745019&quot;,&quot;create_time&quot;:&quot;2021-09-27T03:39:25Z&quot;,&quot;update_time&quot;:&quot;2021-09-27T03:39:25Z&quot;,&quot;links&quot;:[&#123;&quot;href&quot;:&quot;https://api.sandbox.paypal.com/v1/payments/refund/9WW2694970862104R&quot;,&quot;rel&quot;:&quot;self&quot;,&quot;method&quot;:&quot;GET&quot;&#125;,&#123;&quot;href&quot;:&quot;https://api.sandbox.paypal.com/v1/payments/payment/PAYID-MFIBXZQ2F2665569K055620G&quot;,&quot;rel&quot;:&quot;parent_payment&quot;,&quot;method&quot;:&quot;GET&quot;&#125;,&#123;&quot;href&quot;:&quot;https://api.sandbox.paypal.com/v1/payments/sale/1DU410917H5745019&quot;,&quot;rel&quot;:&quot;sale&quot;,&quot;method&quot;:&quot;GET&quot;&#125;]&#125;&#x27;</span>;</span><br><span class="line">            <span class="variable">$result</span> = <span class="variable">$sale</span>-&gt;refundSale(<span class="variable">$refundRequest</span>, <span class="variable">$apiContext</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (\<span class="built_in">Exception</span> <span class="variable">$exception</span>) &#123;</span><br><span class="line">            <span class="keyword">print</span>(<span class="variable">$exception</span>);</span><br><span class="line">            <span class="keyword">exit</span>();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> json_decode(<span class="variable">$result</span>-&gt;toJSON(<span class="number">128</span>), <span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Description</span> 订单通知</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Author</span> 沙屿沫</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Date</span> 2021/9/26 15:39</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> array $data</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> \Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">notify</span>(<span class="params"><span class="keyword">array</span> <span class="variable">$data</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="variable">$demo_data</span> = [</span><br><span class="line">            <span class="string">&#x27;mc_gross&#x27;</span> =&gt; <span class="string">&#x27;0.02&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;invoice&#x27;</span> =&gt; <span class="string">&#x27;614d8f401d847&#x27;</span>,<span class="comment">//</span></span><br><span class="line">            <span class="string">&#x27;protection_eligibility&#x27;</span> =&gt; <span class="string">&#x27;Eligible&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;address_status&#x27;</span> =&gt; <span class="string">&#x27;confirmed&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;payer_id&#x27;</span> =&gt; <span class="string">&#x27;KJSDP2SHURBL4&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;address_street&#x27;</span> =&gt; <span class="string">&#x27;NO 1 Nan Jin Road&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;payment_date&#x27;</span> =&gt; <span class="string">&#x27;01:42:04 Sep 24, 2021 PDT&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;payment_status&#x27;</span> =&gt; <span class="string">&#x27;Completed&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;charset&#x27;</span> =&gt; <span class="string">&#x27;gb2312&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;address_zip&#x27;</span> =&gt; <span class="string">&#x27;200000&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;first_name&#x27;</span> =&gt; <span class="string">&#x27;John&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;mc_fee&#x27;</span> =&gt; <span class="string">&#x27;0.02&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;address_country_code&#x27;</span> =&gt; <span class="string">&#x27;CN&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;address_name&#x27;</span> =&gt; <span class="string">&#x27;Doe John&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;notify_version&#x27;</span> =&gt; <span class="string">&#x27;3.9&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;custom&#x27;</span> =&gt; <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;payer_status&#x27;</span> =&gt; <span class="string">&#x27;verified&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;business&#x27;</span> =&gt; <span class="string">&#x27;sb-oarsk7805857@business.example.com&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;address_country&#x27;</span> =&gt; <span class="string">&#x27;China&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;address_city&#x27;</span> =&gt; <span class="string">&#x27;Shanghai&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;quantity&#x27;</span> =&gt; <span class="string">&#x27;1&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;verify_sign&#x27;</span> =&gt; <span class="string">&#x27;A-on.2dE-HD0drM32ZIE1tBYMyPgAwyGxgTmK4t-mnBRnYKgqGgp0Ens&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;payer_email&#x27;</span> =&gt; <span class="string">&#x27;sb-fkppe7805825@personal.example.com&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;txn_id&#x27;</span> =&gt; <span class="string">&#x27;78H69914TW0115549&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;payment_type&#x27;</span> =&gt; <span class="string">&#x27;instant&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;last_name&#x27;</span> =&gt; <span class="string">&#x27;Doe&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;address_state&#x27;</span> =&gt; <span class="string">&#x27;Shanghai&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;receiver_email&#x27;</span> =&gt; <span class="string">&#x27;sb-oarsk7805857@business.example.com&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;payment_fee&#x27;</span> =&gt; <span class="string">&#x27;0.02&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;shipping_discount&#x27;</span> =&gt; <span class="string">&#x27;0.00&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;insurance_amount&#x27;</span> =&gt; <span class="string">&#x27;0.00&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;receiver_id&#x27;</span> =&gt; <span class="string">&#x27;TY6C65BAP5JD6&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;txn_type&#x27;</span> =&gt; <span class="string">&#x27;express_checkout&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;item_name&#x27;</span> =&gt; <span class="string">&#x27;Testing Request&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;discount&#x27;</span> =&gt; <span class="string">&#x27;0.00&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;mc_currency&#x27;</span> =&gt; <span class="string">&#x27;USD&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;item_number&#x27;</span> =&gt; <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;residence_country&#x27;</span> =&gt; <span class="string">&#x27;CN&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;test_ipn&#x27;</span> =&gt; <span class="string">&#x27;1&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;shipping_method&#x27;</span> =&gt; <span class="string">&#x27;Default&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;transaction_subject&#x27;</span> =&gt; <span class="string">&#x27;Testing Request&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;payment_gross&#x27;</span> =&gt; <span class="string">&#x27;0.02&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;shipping&#x27;</span> =&gt; <span class="string">&#x27;0.00&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;ipn_track_id&#x27;</span> =&gt; <span class="string">&#x27;8aac94e9740a0&#x27;</span>,</span><br><span class="line">        ];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (strtolower(<span class="variable">$data</span>[<span class="string">&#x27;payment_status&#x27;</span>]) === <span class="string">&#x27;completed&#x27;</span>) &#123;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 查询PayPal订单实际支付状态</span></span><br><span class="line">            <span class="variable">$order</span> = <span class="keyword">$this</span>-&gt;query(<span class="variable">$data</span>[<span class="string">&#x27;txn_id&#x27;</span>]);</span><br><span class="line">            <span class="keyword">if</span> (<span class="variable">$order</span>[<span class="string">&#x27;state&#x27;</span>] !== <span class="string">&#x27;completed&#x27;</span>) &#123;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> \<span class="built_in">Exception</span>(<span class="string">&#x27;PayPal订单状态异常&#x27;</span>);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 业务逻辑...</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">沙屿沫</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://blog.shayvmo.cn/2021/09/28/paypal/">http://blog.shayvmo.cn/2021/09/28/paypal/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://blog.shayvmo.cn" target="_blank">指尖改变世界</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Paypal/">Paypal</a></div><div class="post_share"><div class="social-share" data-image="/images/code.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/01/14/iamback/"><img class="prev-cover" src= "" data-lazy-src="/images/code.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">还是回来Hexo啦，方便检索文章</div></div></a></div><div class="next-post pull-right"><a href="/2021/09/27/phpstorm-set-bg-img/"><img class="next-cover" src= "" data-lazy-src="/images/code.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">PHPstorm设置背景图片</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src= "" data-lazy-src="/images/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">沙屿沫</div><div class="author-info__description">科技始终来源于人性</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">74</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">42</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/shayvmo"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">生活就像海洋，只有意志坚强的人才能到达彼岸。</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#PayPal%E6%94%AF%E4%BB%98%E5%AF%B9%E6%8E%A5"><span class="toc-number">1.</span> <span class="toc-text">PayPal支付对接</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%94%AF%E4%BB%98%E6%B5%81%E7%A8%8B"><span class="toc-number">1.1.</span> <span class="toc-text">支付流程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E6%B2%99%E7%AE%B1%E8%B4%A6%E5%8F%B7"><span class="toc-number">1.2.</span> <span class="toc-text">创建沙箱账号</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%BA%94%E7%94%A8"><span class="toc-number">1.3.</span> <span class="toc-text">创建应用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%82%E6%AD%A5%E9%80%9A%E7%9F%A5%E5%9C%B0%E5%9D%80%E8%AE%BE%E7%BD%AE"><span class="toc-number">1.4.</span> <span class="toc-text">异步通知地址设置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.5.</span> <span class="toc-text">示例</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/06/05/webman-route-annotation/" title="webman 框架 注解路由插件"><img src= "" data-lazy-src="/images/code.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="webman 框架 注解路由插件"/></a><div class="content"><a class="title" href="/2023/06/05/webman-route-annotation/" title="webman 框架 注解路由插件">webman 框架 注解路由插件</a><time datetime="2023-06-05T07:04:07.000Z" title="发表于 2023-06-05 15:04:07">2023-06-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/05/29/nginx-reload-not-effect/" title="windows服务器重载nginx配置无效"><img src= "" data-lazy-src="/images/code.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="windows服务器重载nginx配置无效"/></a><div class="content"><a class="title" href="/2023/05/29/nginx-reload-not-effect/" title="windows服务器重载nginx配置无效">windows服务器重载nginx配置无效</a><time datetime="2023-05-29T08:03:45.000Z" title="发表于 2023-05-29 16:03:45">2023-05-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/05/24/php-aes/" title="前后端接口传输AES加密"><img src= "" data-lazy-src="/images/code.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="前后端接口传输AES加密"/></a><div class="content"><a class="title" href="/2023/05/24/php-aes/" title="前后端接口传输AES加密">前后端接口传输AES加密</a><time datetime="2023-05-24T07:00:39.000Z" title="发表于 2023-05-24 15:00:39">2023-05-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/08/16/yar-demo/" title="Yar拓展，实现PHP的RPC服务"><img src= "" data-lazy-src="/images/code.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Yar拓展，实现PHP的RPC服务"/></a><div class="content"><a class="title" href="/2022/08/16/yar-demo/" title="Yar拓展，实现PHP的RPC服务">Yar拓展，实现PHP的RPC服务</a><time datetime="2022-08-16T09:30:43.000Z" title="发表于 2022-08-16 17:30:43">2022-08-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/08/15/my-online-log/" title="线上日志系统"><img src= "" data-lazy-src="/images/code.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="线上日志系统"/></a><div class="content"><a class="title" href="/2022/08/15/my-online-log/" title="线上日志系统">线上日志系统</a><time datetime="2022-08-15T09:03:01.000Z" title="发表于 2022-08-15 17:03:01">2022-08-15</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/images/code.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2019 - 2023 By 沙屿沫</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a href="http://www.beian.miit.gov.cn/" target="_blank" rel="noopener"><img class="icp-icon" src= "" data-lazy-src="/images/icp.png"><span>粤ICP备18133330号</span></a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="false" data-text="富强,民主,文明,和谐,自由,平等,公正,法治,爱国,敬业,诚信,友善" data-fontsize="15px" data-random="false" async="async"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>